#include "plot.h"
#include <qwt_color_map.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_plot_layout.h>
#include <qwt_matrix_raster_data.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_canvas.h>

#include <QFileDialog>
#include <QStandardPaths>

class ColorMap: public QwtLinearColorMap
{
public:
    ColorMap():
        QwtLinearColorMap( Qt::darkBlue, Qt::darkRed )
    {
        addColorStop( 0.2, Qt::blue );
        addColorStop( 0.4, Qt::cyan );
        addColorStop( 0.6, Qt::yellow );
        addColorStop( 0.8, Qt::red );
    }
};

Plot::Plot( QWidget *parent ):
    QwtPlot( parent )
{
    type = RECORD_TEMP_AT_TIME;

    d_spectrogram = new QwtPlotSpectrogram();
    d_spectrogram->setRenderThreadCount( 0 ); // use system specific thread count
    d_spectrogram->setColorMap(new ColorMap());
    d_spectrogram->setData(new QwtMatrixRasterData());
    d_spectrogram->attach(this);

    d_curve = new QwtPlotCurve();
    d_curve->setRenderThreadCount(0);
    d_curve->setRenderHint(QwtPlotItem::RenderAntialiased);

    QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
    rightAxis->setColorBarEnabled( true);
    rightAxis->setColorBarWidth( 40 );
    rightAxis->setColorMap( QwtInterval(0,1000), new ColorMap());

    setAxisScale( QwtPlot::yRight, 0, 1000 );
    enableAxis( QwtPlot::yRight );
    setAxisScale( QwtPlot::xBottom, 0.0, 8.0);
    setAxisMaxMinor( QwtPlot::xBottom, 0 );
    setAxisScale( QwtPlot::yLeft, 0.0, 8.0 );
    setAxisMaxMinor( QwtPlot::yLeft, 0 );

}


void Plot::setData(const Record & data, int cols){
    if (data.type == RECORD_TEMP_AT_TIME){
        auto rasterData = static_cast<QwtMatrixRasterData *>(d_spectrogram->data());
        rasterData->setValueMatrix(data.data, cols);
        if (type!=data.type){
            d_curve->detach();
            d_spectrogram->attach(this);
        }
        type = data.type;

    }else if (data.type == RECORD_TEMP_AT_POINT) {
        auto x = QVector<double>(data.data.size());
        for (int i=0;i<x.size();i++){
            x[i] = i*m_param.dt;
        }
        d_curve->setSamples(x,data.data);
        if (type!=data.type){
            d_spectrogram->detach();
            d_curve->attach(this);
        }
        type = data.type;
    }
}

void Plot::exportPlot()
{
    QwtPlotRenderer renderer;
    renderer.exportTo( this, "data.svg" );

}

void Plot::setResampleMode( int mode )
{
    auto *rasterData = static_cast<QwtMatrixRasterData *>( d_spectrogram->data() );
    rasterData->setResampleMode(
                static_cast<QwtMatrixRasterData::ResampleMode>( mode ) );

    replot();
}


void Plot::setParameter(const Parameter & parm,int stop){
    m_param = parm;
    d_spectrogram->data()->setInterval(Qt::XAxis,QwtInterval( -parm.dx/2, parm.xNums*parm.dx-parm.dx/2));
    d_spectrogram->data()->setInterval(Qt::YAxis,QwtInterval(-parm.dy/2, parm.yNums*parm.dy-parm.dy/2));
    d_spectrogram->data()->setInterval(Qt::ZAxis,QwtInterval(qMin(parm.t0,parm.tw),qMax(parm.t0,parm.tw)));
    if (type == RECORD_TEMP_AT_TIME){
        setAxisScale( QwtPlot::xBottom, 0.0, parm.l1 );
        setAxisScale( QwtPlot::yLeft, 0.0, parm.l2 );
    }else if(type == RECORD_TEMP_AT_POINT) {
        setAxisScale(QwtPlot::yLeft,qMin(parm.t0,parm.tw),qMax(parm.t0,parm.tw));
        if (stop>0) {
            setAxisScale(QwtPlot::xBottom,0,parm.stop*parm.dt);
        }
    }
    replot();
}
